<?php
/**
 * 网管管理类
 *
 * @auther 大耳牛
 * 2016-12-31
 **/
namespace Xijie\Model;
use Think\Model;
class RoomModel extends Model{
	//定义表名称
	protected $tableName = 'room';
	
	/**
	 * 获取房间列表总数
	 *
	 **/
	public function getRoomListCount(){
		return $this->count();
	}
	
	/**
	 * 获取房间列表
	 *
	 **/
	public function getRoomList($page){
		return $this->alias('room')
					//->join('left join momoda_lock as slock on room.room_id = slock.room_id')
					//->join('left join momoda_network as network on slock.network_id = network.network_id')
					->field('room.*')
					->order('room.room_number/1 asc')
					->page($page, C('PAGE_SIZE'))
					->select();
	}
	
	/**
	 * 房间信息保存
	 *
	 **/
	public function addRoomInfo($data){
		$this->data($data)->add();
	}
	
	/**
	 * 删除房间信息
	 * @param int $room_id 房间ID
	 **/
	public function delRoomInfo($room_id){
		return $this->where(array('room_id' => $room_id))->delete();
	}
	
	/**
	 * 编辑房间信息
	 * @param int $room_id
	 * @param Array $data 编辑资料
	 **/
	public function editRoomInfo($room_id, $data){
		return $this->where(array('room_id' => $room_id))->save($data);
	}
	
	/**
	 * 查询房间详情
	 * @param int $room_id
	 **/
	public function getRoomInfoById($room_id){
		return $this->where(array('room_id' => $room_id))->find();
	}
	
	public function getRoomInfoByNumber($room_number){
		return $this->where(array('room_number' => $room_number))->find();
	}

	/**
	 * 房间租金规则
	 **/
	public function addRoomRent($data){
		return M('room_rent')->addAll($data);
	}

	/**
	 * 根据条件查询享受的规则
	 * @param string $roomnumber 房间号
	 * @param int $btime 签约时间
	 **/
	public function getRoomRentByGuiZe($roomnumber, $btime){
		return M('room_rent')->where(array(
			'roomnumber' => $roomnumber,
			'begintime' => array('elt', $btime),
			'endtime' => array('egt', $btime)
		))->order('id desc')->limit(1)->find();
	}

	/**
	 * 获取规则
	 **/
	public function getRoomRentByRoomNumber($room_number){
		return M('room_rent')->where(array('roomnumber' => $room_number))->order('id asc')->select();
	}

	public function deleteRoomRentByRoomNumber($room_number){
		return M('room_rent')->where(array('roomnumber' => $room_number))->delete();
	}
}